package csel.model;

import java.util.*;

public class GameData implements java.io.Serializable, Observer
{
	/**
	 * Change this if we change its structure.
	 */
	private static final long serialVersionUID = 2L;

	private final GameGlobals globals;
	private final List<Entity> entities;
	private Entity playerEntity;
	private final Map map;
	
	public static GameData makeGameData(GameGlobals globals, List<Entity> entities, Entity playerEntity, Map map)
	{
		GameData gd;
		
		gd = new GameData(globals, entities, playerEntity, map);
		playerEntity.addObserver(gd);
		
		return gd;
	}

	protected GameData(GameGlobals globals, List<Entity> entities, Entity playerEntity, Map map)
	{
		this.globals = globals;
		this.entities = entities;
		this.playerEntity = playerEntity;
		this.map = map;
		
		for (Entity entity : entities)
		{
			globals.getChronoTrigger().addTicked(entity);
		}
	}
	
	/**
	 * Cleanup any game state. THIS MUST BE CALLED BEFORE LOSING REFERENCE OR BAD THINGS HAPPEN.
	 */
	public void cleanup()
	{
		getChronoTrigger().stop();
		playerEntity.removeObserver(this);
	}

	public Entity get_playerEntity()
	{
		return playerEntity;
	}

	public void set_playerEntity(Entity entity)
	{
		playerEntity = entity;
	}

	public List<Entity> get_entities()
	{
		return entities;
	}

	public Map get_map()
	{
		return map;
	}
	
	public ChronoTrigger getChronoTrigger()
	{
		return globals.getChronoTrigger();
	}

	public void modelUpdate(Object subject)
	{
		if (subject == playerEntity)
		{
			if (playerEntity.getLives() <= 0)
				getChronoTrigger().stop();
		}
	}
}
